﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace GameHungTrung
{
    public class Basket
    {
        private My2DSprite _roSprite;

        private MouseState mouseState;

        Vector2 updateVector;

        Vector2 oldPos;

        public Vector2 XY_RegionCollision { private set; get; }
        public Vector2 WH_RegionCollision { private set; get; }

        public Basket()
        {
            mouseState = new MouseState();
            mouseState = Mouse.GetState();
            oldPos = new Vector2(mouseState.X, mouseState.Y);
        }

        public void LoadContent(ContentManager content)
        {
            Texture2D[] tmpTexture;
            Vector2 tmpVector;

            // ro
            tmpTexture = new Texture2D[1];
            tmpTexture[0] = content.Load<Texture2D>(Global.PATH_FILE_IMAGE + "basket");

            tmpVector = new Vector2(0.0f, 450.0f);           

            _roSprite = new My2DSprite(tmpTexture, tmpVector);            
        }

        bool isPressed_Left = false, isPressed_Right = false;
        bool move_left = false, move_right = false;
        bool keyboard_move = false;

        public void Update(GameTime gameTime)
        {
            mouseState = Mouse.GetState();
            

            // tinh vi tri ro di chuyen
            if ((oldPos.X - mouseState.X == 0) && (oldPos.Y - mouseState.Y) == 0)
            {
                keyboard_move = true;
            }
            else
            {
                updateVector = new Vector2(
                    mouseState.X - _roSprite.getWidthTexture() / 2,
                    _roSprite.Position.Y);
                keyboard_move = false;
                oldPos = new Vector2(mouseState.X,mouseState.Y);
            }

            if (keyboard_move == true)
            {
                KeyboardState ks = Keyboard.GetState();

                updateVector.Y = _roSprite.Position.Y;
                //// qua trai
                if (ks.IsKeyDown(Keys.Left) && isPressed_Left == false)
                {
                    isPressed_Left = true;
                    move_left = true;
                }
                if (ks.IsKeyUp(Keys.Left))
                {
                    if (isPressed_Left)
                    {
                        isPressed_Left = false;
                        move_left = false;

                    }
                }

                if (move_left == true)
                    updateVector = new Vector2(
                        _roSprite.Position.X - 10,
                        _roSprite.Position.Y);

                //// qua phai
                if (ks.IsKeyDown(Keys.Right) && isPressed_Right == false)
                {
                    isPressed_Right = true;
                    move_right = true;
                }
                if (ks.IsKeyUp(Keys.Right))
                {
                    if (isPressed_Right)
                    {
                        isPressed_Right = false;
                        move_right = false;
                    }
                }

                if (move_right == true)
                    updateVector = new Vector2(
                            _roSprite.Position.X + 10,
                            _roSprite.Position.Y);
            }

            if(updateVector.X <= 0)
                updateVector.X = 0;
            if(updateVector.X + _roSprite.getWidthTexture() >= Global.SCREEN_WIDTH)
                updateVector.X = Global.SCREEN_WIDTH - _roSprite.getWidthTexture();

            _roSprite.UpdatePosition(updateVector);


            //XY_RegionCollision = new Vector2(updateVector.X + _roSprite.getWidthTexture() / 10,
            //    updateVector.Y +  _roSprite.getHeightTexture() / 2);

            XY_RegionCollision = new Vector2(updateVector.X,
               updateVector.Y + _roSprite.getHeightTexture() / 2);

            //WH_RegionCollision = new Vector2(_roSprite.getWidthTexture() - 2 * _roSprite.getWidthTexture() / 10,
            //    _roSprite.getHeightTexture() / 2);

            WH_RegionCollision = new Vector2(_roSprite.getWidthTexture(),
                _roSprite.getHeightTexture() / 2);

            _roSprite.Update(gameTime);
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            _roSprite.Draw(gameTime, spriteBatch);
        }
        
    }
}
